#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/6/21 0021 11:22
# @Author  : wzy
# @Site    : 
# @File    : run_case_ddt.py
# @Software: PyCharm

from Util.operate_excel import operateExcel
import json
from Util.operate_header import get_header,get_header_value
from Util.operate_result import operate_result,get_result_json,cmp_result_json
from Util.operate_cookie import get_cookie_value,write_cookie_value
from Util.data_codition import get_depend_data
from Base.base_request import request
from Util.operate_pathUrll import pathinfo
import Base.HTMLTestRunner as HTMLTestRunner
import unittest
import ddt
data = operateExcel.get_excel_data()
# ['imooc_001',
# '登陆',
# 'yes', None,
# 'login',
# 'post',
# '{"username":"111111"}',
# 'yes',
# 'message',
# None]
@ddt.ddt
class TestRunCaseDdt(unittest.TestCase):
    '''
    ddt
    '''
    @ddt.data(*data)  # 获取所有测试用， 并且一行一行的执行  A2->A9 一行算是一个test_case
    def test_main_case(self,data):
        print(data)
        cookie = None
        get_cookie = None
        header = None
        depend_data = None
        is_run = data[2]
        case_id = data[0]
        i = operateExcel.get_rows_number(case_id)
        if is_run == 'yes':
            is_depend = data[3]
            data1 = json.loads(data[7])
            try:
                if is_depend:
                    '''
                    获取依赖数据
                    '''
                    depend_key = data[4]
                    depend_data = get_depend_data(is_depend)
                    #print(depend_data)
                    data1[depend_key] = depend_data

                method = data[6]
                url = data[5]

                is_header = data[9]
                excepect_method = data[10]
                excepect_result = data[11]
                cookie_method = data[8]
                if cookie_method == 'yes':
                    cookie = get_cookie_value('is_cookie')
                if cookie_method == 'write':
                    '''
                    必须是获取到cookie
                    '''
                    get_cookie = {"is_cookie": "is_cookie"}
                if is_header == 'yes':
                    header = get_header()

                res = request.run_main(method, url, data1, cookie, get_cookie, header)
                code = str(res['code'])
                message = res['message']

                # 如果是mec  格式，通过将result = 通过（*13） 并且 写入数据（*14）作为后边单元格的数据依赖
                if excepect_method == 'mec':
                    config_message = operate_result(url, code)
                    try:
                        self.assertEqual(message, config_message)
                        operateExcel.excel_write_data(i, 13, "通过")
                        operateExcel.excel_write_data(i, 14, json.dumps(res))
                    except Exception as e:
                        operateExcel.excel_write_data(i, 13, "失败")
                        raise e

                # 如果只是单纯的错误类型 只修改result（*13）状态
                if excepect_method == 'errorcode':
                    try:
                        self.assertEqual(excepect_result, code)
                        operateExcel.excel_write_data(i, 13, "通过")
                    except Exception as e:
                        operateExcel.excel_write_data(i, 13, "失败")
                        raise e

                #如果是JSON格式
                if excepect_method == 'json':

                    if code == 200:
                        status_str = 'sucess'
                    else:
                        status_str = 'error'
                    excepect_result = get_result_json(url, status_str)
                    result = operate_result(res, excepect_result)
                    try:
                        self.assertTrue(result)
                        operateExcel.excel_write_data(i, 13, "通过")
                    except Exception as e:
                        operateExcel.excel_write_data(i, 13, "失败")
                        raise e
            except Exception as e:
                operateExcel.excel_write_data(i, 13, "失败")
                raise e

    def setUp(self) -> None:
        print("开始当前用例")

    def tearDown(self) -> None:
        print("开始当前用例")

    @classmethod
    def setUpClass(cls) -> None:
        print("开始执行用例")

    @classmethod
    def tearDownClass(cls) -> None:
        print("结束执行用例")


    def test_01(self):
        print("执行case01")

    def test_02(self):
        print("执行case02")


if __name__ == '__main__':
    pass

